﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.Collections.Generic;

public partial class UserControl_ucSoSanh : System.Web.UI.UserControl
{
    TblItemsDAO ItemsDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblItemDetailDAO itemDetailDAO = new TblItemDetailDAO(Global.CONNECTION_STRING);
    string id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDetails();
            spsosanh();
            load_cb_sp();
        }
    }
    
    private void spsosanh()
    {
        id = Request.QueryString["id"];
        Repeater1.DataSource = itemDetailDAO.GetDetailsItems(Convert.ToInt32(id));
        Repeater1.DataBind();
    }
    private void load_cb_sp()
    {
        TblItemsDAO itemDAO = new TblItemsDAO(Global.CONNECTION_STRING);
        DataTable dt = itemDAO.GetItems();
        cbsp.DataSource = dt;
        cbsp.DataTextField = "ItemName";
        cbsp.DataValueField = "ItemID";
        cbsp.DataBind();
       
    }
    private void LoadDetails()
    {
        string id = Request.QueryString["id"];
        //frmViewDetail.DataSource = ItemsDAO.GetListItems(Convert.ToInt32(id), "", "", 1, 10, "");
        frmViewDetailSosanh1.DataSource = ItemsDAO.GetvItemByID(Convert.ToInt32(id));
        frmViewDetailSosanh1.DataBind();
    }

    protected void addcart_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Label lblInfoAddCart1 = (Label)frmViewDetailSosanh1.FindControl("lblInfoAddCart");
            lblInfoAddCart1.Text = "";

            string id = Request.QueryString["id"];
            BasePage.SetShoppingCartSesstion(id + "|0");

            List<TblOrderDetail> arrItems = new List<TblOrderDetail>();
            if (Session[Global.SESSION_CART] != null)
                arrItems = (List<TblOrderDetail>)Session[Global.SESSION_CART];

            int iNoOfItems = 0;
            double dTotalAmount = 0;

            foreach (TblOrderDetail order in arrItems)
            {
                iNoOfItems += order.Quantity;
                dTotalAmount += Convert.ToDouble(order.Quantity) * Convert.ToDouble(order.Price);
            }
            lblInfoAddCart1.Text = "Đã thêm vào giỏ hàng ! ";
            ScriptManager.RegisterStartupScript(frmViewDetailSosanh1,
                this.frmViewDetailSosanh1.GetType(), "ScriptUpdateParents", "UpdateParentsCart('" + iNoOfItems.ToString() + "|" + dTotalAmount.ToString("0,00.##") + "');", true);
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }

    protected void addcartss_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Label lblInfoAddCart2 = (Label)frmViewDetail1.FindControl("lblInfoAddCartss");
            lblInfoAddCart2.Text = "";

            string id = cbsp.SelectedValue.ToString();
            BasePage.SetShoppingCartSesstion(id + "|0");

            List<TblOrderDetail> arrItems = new List<TblOrderDetail>();
            if (Session[Global.SESSION_CART] != null)
                arrItems = (List<TblOrderDetail>)Session[Global.SESSION_CART];

            int iNoOfItems = 0;
            double dTotalAmount = 0;

            foreach (TblOrderDetail order in arrItems)
            {
                iNoOfItems += order.Quantity;
                dTotalAmount += Convert.ToDouble(order.Quantity) * Convert.ToDouble(order.Price);
            }
            lblInfoAddCart2.Text = "Đã thêm vào giỏ hàng ! ";
            //ScriptManager.RegisterStartupScript(updatepanel, updatepanel.GetType(), "ScriptUpdateParents", "UpdateParentsCart('" + iNoOfItems.ToString() + "|" + dTotalAmount.ToString("0,00.##") + "');", true);
            ScriptManager.RegisterStartupScript(frmViewDetail1,
                this.frmViewDetail1.GetType(), "ScriptUpdateParents", "UpdateParentsCart('" + iNoOfItems.ToString() + "|" + dTotalAmount.ToString("0,00.##") + "');", true);
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }
    protected void btnvote_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];

        Label lbVote = (Label)frmViewDetailSosanh1.FindControl("lbvote");
        int lb = Global.ConvertToInt(lbVote.Text);
        lb += 1;
        lbVote.Text = lb.ToString();

        TblItemDetail item_detail = new TblItemDetail();

        item_detail.Vote = lb;
        itemDetailDAO.UpdateVote(Global.ConvertToInt(id), lb);
    }

    protected void btnvotess_Click(object sender, ImageClickEventArgs e)
    {


        Label lbVote_ss = (Label)frmViewDetail1.FindControl("lbvotess");
        int lb = Global.ConvertToInt(lbVote_ss.Text);
        lb += 1;
        lbVote_ss.Text = lb.ToString();

        TblItemDetail item_detail = new TblItemDetail();

        item_detail.Vote = lb;
        itemDetailDAO.UpdateVote(Global.ConvertToInt(cbsp.SelectedValue.ToString()), lb);
    }

    protected void cbsp_SelectedIndexChanged(object sender, EventArgs e)
    {
        frmViewDetail1.DataSource = ItemsDAO.GetvItemByID(Global.ConvertToInt(cbsp.SelectedValue.ToString()));
        frmViewDetail1.DataBind();
        rpTinhNangSP.DataSource = itemDetailDAO.GetDetailsItems(Global.ConvertToInt(cbsp.SelectedValue.ToString()));
        rpTinhNangSP.DataBind();
    }
}
